package logic

import (
	"context"
	"order/models/mysql"

	"order/orderrpc/internal/svc"
	"order/orderrpc/orderrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderListLogic {
	return &OrderListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderListLogic) OrderList(in *orderrpc.OrderListRequest) (*orderrpc.OrderListResponse, error) {
	// todo: add your logic here and delete this line
	var orders []mysql.Order
	mysql.DB.Debug().Order("price desc").First(&orders)

	var ListOrder []*orderrpc.OrderList
	for _, order := range orders {
		list := orderrpc.OrderList{
			Name:   order.Name,
			Price:  float32(order.Price),
			Number: int64(order.Number),
			Status: order.Status,
		}
		ListOrder = append(ListOrder, &list)
	}
	return &orderrpc.OrderListResponse{
		List: ListOrder,
	}, nil
}
